import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import Login from "../views/Login.vue"
import Homejd from "../views/Mysettings/homejd.vue"
import MyBalance from "../views/Mysettings/MyBalance.vue"
import Pwd from "../views/Mysettings/pwd.vue"
import settings from "../views/Mysettings/Settings.vue"
import Register from "../views/register.vue"
import CArt from "../views/more/Cart.vue"
import Orditem from "../views/more/orditem.vue"
import Order from "../views/more/order.vue"
import ADm from "../views/adm/adm.vue"
import ORder from "../views/adm/order/order.vue"
import Sller from "../views/adm/seller/Show.vue"
import add from "../views/adm/seller/add.vue"
import oneType from "../views/adm/type/onetype.vue"
import twotype from "../views/adm/type/twotype.vue"
import goods from "../views/adm/good/Goods.vue"
import addgood from "../views/adm/good/addgoods.vue"
import Carousel from "../views/carousel/Carousel.vue"
import Buyer from "../views/buyer/buyer.vue"
import BuyerPwd from "../views/buyer/pwd.vue"
import Shopping from "../views/seller/shopping.vue"
import good from "../views/seller/good.vue"

const routes = [
  { path: "/", redirect: "/login" },
  {
    path: "/login",
    component: Login,
    meta: { public: true } // 标记公共页面
  },
  {
    path: "/register",
    component: Register,
    meta: { public: true } // 补充公共页面标记
  },
  {
    path: "/homejd",
    component: Homejd,
    meta: { role: "seller" }, // 设置权限角色（如 "seller"）
    children: [
      { path: "myBalance", component: MyBalance },
      { path: "Pwd", component: Pwd },
      { path: "settings", component: settings }
    ]
  },
  { path: "/rat", component: CArt, meta: { role: "seller" } }, // 示例：购物车需登录
  { path: "/order", component: Order, meta: { role: "seller" } },
  { path: "/orditem", component: Orditem, meta: { role: "seller" } },
  {
    path: "/adm",
    component: ADm,
    meta: { role: "adm" }, // 管理员权限
    children: [
      { path: "sller", component: Sller },
      { path: "add", component: add },
      { path: "onetype", component: oneType },
      { path: "twotype", component: twotype },
      // { path: "pwd", component: pwd },
      { path: "Goods", component: goods },
      { path: "addgoods", component: addgood },
      { path: "carousel", component: Carousel },
      { path: "order", component: ORder }
    ]
  },
  {
    path: "/buyer",
    component: Buyer,
    meta: { role: "seller" }, 
    children: [{ path: "pwd", component: BuyerPwd }]
  },
  {
    path: "/seller",
    component: Login, // 此处可能为逻辑错误，买家页面不应重定向到登录页
    meta: { public: true }, // 若为公共页面，需标记
    children: [
      { path: "shopping", component: Shopping },
      { path: "good", component: good }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  console.log(`当前访问路径：${to.path}，需要角色：${to.meta.role}`)
  const role = localStorage.getItem("role") || "" // 处理null情况，转为空字符串
  const publicPages = ["/", "/login", "/register"] // 白名单包含所有公共页面

  // 公共页面直接放行
  if (publicPages.includes(to.path)) return next()

  // 未登录处理
  if (!role) {
    ElMessage.error("请先登录")
    return next("/login")
  }

  // 权限验证：路由需设置meta.role，且与用户角色匹配
  if (to.meta.role && to.meta.role !== role) {
    ElMessage.error("权限不足，无法访问")
    return next(from.path || "/login") // 无权限时返回上一页或登录页
  }

  next()
})

export default router